<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span='24'>
            <a-form-item label='项目简称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                v-decorator="['projectName',validatorRules.projectShortName]"
                :trigger-change='true'
                org-fields='id,short_name'
                dest-fields='projectId,projectName'
                code='pm_project_select'
                @callback='popupCallback'
              />
              <input v-decorator="['projectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectCode']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='专题名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                v-decorator="['subjectName',validatorRules.subjectName]"
                :trigger-change='true'
                org-fields='id,subject_name,subject_code'
                dest-fields='subjectId,subjectName,subjectCode'
                code='pm_subject_select'
                :param='{projectId: model.projectId}'
                @callback='popupCallback'
              />
              <input v-decorator="['subjectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="专题编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['subjectCode']" disabled placeholder="请选择专题编号"></a-input>
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-item label="交付物编号" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <a-input v-decorator="['recordCode', validatorRules.recordCode]" placeholder="请输入交付物编号"></a-input>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-item label="交付物名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['recordDep', validatorRules.recordDep]" placeholder="请输入交付物名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="交付物描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['recordDes']" rows="4" placeholder="请输入交付物描述"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <hf-project-select-modal ref="hfProjectSelectModal" :multiple="false" @hf-pm-project-select="hfPmProjectSelect"/>
    <hf-subject-select-modal ref="hfSubjectSelectModal" :multiple="false" @hf-subject-select="hfSubjectSelect"/>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'
import pick from 'lodash.pick'
import {validateDuplicateValue} from '@/utils/util'
import HfProjectSelectModal from '@views/hifar/pm/modules/HfProjectSelectModal'
import HfSubjectSelectModal from '@views/hifar/pm/modules/HfSubjectSelectModal'

export default {
  name: 'HfPmDestinationFileRecordsForm',
  components: {
    HfProjectSelectModal,
    HfSubjectSelectModal
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        recordCode: {
          rules: [
            {required: true, message: '请输入交付物编号!'}
          ]
        },
        recordDep: {
          rules: [
            {required: true, message: '请输入交付物名称!'}
          ]
        },
        projectShortName: {
          rules: [
            {required: true, message: '请选择项目简称!'}
          ]
        },
        subjectName: {
          rules: [
            {required: true, message: '请选择专题名称!'}
          ]
        }
      },
      url: {
        add: "/pm/hfPmDestinationFileRecords/add",
        edit: "/pm/hfPmDestinationFileRecords/edit",
        queryById: "/pm/hfPmDestinationFileRecords/queryById"
      },
      defaultModel: {}
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    add() {
      let defaultObj = {}
      let {
        selectedSubName,
        selectedName,
        selectedKeys,
        selectedSubKeys,
        selectedCode,
        selectedSubCode,
        selectedTreeType
      } = this.defaultModel
      if (selectedTreeType === 'project') {
        defaultObj = {projectName: selectedName, projectId: selectedKeys, projectCode: selectedCode}
      } else if (selectedTreeType === 'subject') {
        defaultObj = {
          projectName: selectedName,
          projectId: selectedKeys,
          projectCode: selectedCode,
          subjectName: selectedSubName,
          subjectCode: selectedSubCode,
          subjectId: selectedSubKeys,
        }
      }
      this.edit(defaultObj);
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'createBy', 'createTime', 'remarks', 'recordCode', 'recordDep', 'recordDes',
            'projectId', 'projectName', 'projectCode', 'subjectId', 'subjectName', 'subjectCode'))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = {id: this.formData.dataId};
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'createBy', 'createTime', 'remarks', 'recordCode', 'recordDep', 'recordDes',
          'projectId', 'projectName', 'projectCode', 'subjectId', 'subjectName', 'subjectCode'))
    },
    projectShortNameClick() {
      this.$nextTick(() => {
        this.$refs.hfProjectSelectModal.title = '选择项目'
        this.$refs.hfProjectSelectModal.visible = true
      })
    },
    hfPmProjectSelect(data) {
      this.form.setFieldsValue({
        projectId: data.id,
        projectName: data.shortName,
        projectCode: data.code
      })
    },
    subjectNameClick() {
      this.$nextTick(() => {
        this.$refs.hfSubjectSelectModal.title = '选择专题'
        this.$refs.hfSubjectSelectModal.visible = true
      })
    },
    hfSubjectSelect(data) {
      this.form.setFieldsValue({
        subjectId: data.id,
        subjectName: data.subjectName,
        subjectCode: data.subjectCode
      })
    }
  }
}
</script>